import { Application } from 'egg';

export default (app: Application) => {
  const { controller, router } = app;
  // validator 
  app.validator.addRule('dateString',(_, value)=>{
    try {
      if ( Date.parse(value).toString() == 'Invalid Date' ) throw Error;
    } catch (err) {
      return 'must be a Date String'
    }
  })
  // visitor router
  router.get('/', controller.home.index);
  router.get('/API/V1/articles', controller.articles.index);
  router.get('/API/V1/articles/:id', controller.articles.byId);
  router.get('/API/V1/categories', controller.categories.index);
  router.get('/API/V1/categories/statistics', controller.categories.statistics);
  router.post('/API/V1/admin/users/login', controller.admin.users.login);
  router.put('/API/V1/admin/users/:id', controller.admin.users.modifyPassword);
  router.post('/API/V1/admin/articles', controller.articles.newArticle);
  router.put('/API/V1/admin/articles/:id', controller.articles.modify);
  router.delete('/API/V1/admin/articles/:id', controller.articles.deleteById);
  router.post('/API/V1/admin/articles/:id/images', controller.articles.uploadImage);
};
